# -*- encoding=utf8 -*-
__author__ = "chen"

from datetime import datetime

from common.common_util import *

def get_yabiao_task(hwnd):
    # 获取押镖 任务弹窗
    # 找到押镖，返回坐标，否则返回空
    connect_device(f"Windows:///{hwnd}")
    try:
        pcos = wait(Template(r"yabiao_task_putong.png", record_pos=(0.33, 0.1), resolution=(635, 503)), 1)
        return hwnd,pcos
    except :
        print("未找到押镖任务弹窗")
        return None
def gaoji_yaobiao(hwnd):
    '''
    :param hwnd: 对应窗口的句柄值
    :return: 返回高级押镖次数
    '''
    connect_device(f"Windows:///{hwnd}")
    #点击 高级押镖
    try:
        pcos = wait(Template(r"tpl1717680264268.png", record_pos=(0.332, 0.154), resolution=(494, 398)))
        x = random.randint(-20, 20)
        y = random.randint(-2, 2)
        sleep(1)
        touch((pcos[0] + x, pcos[1] + y))

        pcos = wait(Template(r"sure_ok.png", record_pos=(0.091, 0.051), resolution=(635, 503)))
        x = random.randint(-5, 5)
        y = random.randint(-2, 2)
        sleep(1)
        touch((pcos[0] + x, pcos[1] + y))
        return True
    except:
        print("xxxxxxxxxxxxxxxxxxxxxxxxx")
        return False
def putong_yaobiao(hwnd):
    '''
    :param hwnd: 对应窗口的句柄值
    :return: 返回高级押镖次数
    '''
    connect_device(f"Windows:///{hwnd}")
    # 点击 普通押镖
    try:
        pcos = wait(Template(r"tpl1717680055268.png", record_pos=(0.332, 0.097), resolution=(494, 398)))
        x = random.randint(-20, 20)
        y = random.randint(-1, 1)
        sleep(1)
        touch((pcos[0] + x, pcos[1] + y))
    # 点击确定
        pcos = wait(Template(r"sure_ok.png", record_pos=(0.091, 0.051), resolution=(635, 503)))
        x = random.randint(-20, 20)
        y = random.randint(-1, 1)
        sleep(1)
        touch((pcos[0] + x, pcos[1] + y))
        return True
    except:
        print("xxxxxxxxxxxxxxxxxxxxxxxxx")
        return False
def run_task(hwnd_list,gj_couter,pt_couter,is_gj):

    hwnds = get_hwnd_from_title(hwnd_list)

    record = []
    # 获取所有窗口的
    for i in range(len(hwnds)):
        if is_gj == 1:
            record.append([hwnds[i],0,0,True]) # 配置0，执行高级押镖
        else:
            record.append([hwnds[i],3,0,True])  # 配置3，不执行高级押镖
    # 押镖

    for j in range(1000):
        for i in range(len(hwnds)):
            if record[i][3]:
                hp = get_yabiao_task(hwnds[i])
                print(record)
                print(f"押镖窗口信息：{hp}")
                # 高级押镖判断
                if hp:
                    if record[i][1] < gj_couter:
                        if gaoji_yaobiao(hp[0]):
                            record[i][1] += 1
                    # 普通押镖
                    if record[i][2] < gj_couter:
                        if putong_yaobiao(hp[0]):
                            record[i][2] += 1
        sleep(100/len(hwnds))
        bl = True
        for i in range(len(hwnds)):
            bl1 = record[i][1] == gj_couter and record[i][2] == pt_couter
            if bl1:
                record[i][3] = False
            bl = (bl and bl1)
        if bl:
            break
        print(record)

if __name__ == '__main__':
    # part1:开多个进程,每个进程都有不同的pid
    gj_couter = 3  # 普通押镖次数
    pt_couter = 3  # 高级押镖次数
    is_gj = 0  # 1--高级押镖，0--不进行高级押镖
    hwnd_list = [2,3,4,5,6,7,8,9,10]
    hwnd_list = []
    run_task(hwnd_list,gj_couter,pt_couter,is_gj)




